package com.example.fq.s3;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fq on 15-5-6.
 */
public class CarFragment extends Fragment implements ListView.OnItemClickListener {

    private ListView carListView;
    private List<Car> carList;
    private CarAdapter adapter;
    private boolean isTwoPane;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        carList = initCar();
        adapter = new CarAdapter(activity, R.layout.car_item, carList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.car, container, false);
        carListView = (ListView) view.findViewById(R.id.car_list_view);
        carListView.setAdapter(adapter);
        carListView.setOnItemClickListener(this);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        if (getActivity().findViewById(R.id.news_content_layout)!=null){
            isTwoPane = true;
        }else{
            isTwoPane = false;
        }
    }
//
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position,
//                            long id) {
//        Car car = carList.get(position);
//        if (isTwoPane) {
//            NewsContentFragment newsContentFragment = (NewsContentFragment)
//                    getFragmentManager().findFragmentById(R.id.news_content_fragment);
//            newsContentFragment.refresh(car.getStyle(), car.getPrice());
//        } else {
//            Log.w("debug2", "2");
//            NewsContentActivity.actionStart(getActivity(), car.getStyle(),
//                    car.getPrice());
//        }
//    }

    private List<Car> initCar() {
        List<Car> carList = new ArrayList<Car>();
//        Car car1 = new Car();
//        car1.setStyle("this is the title 01");
//        car1.setPrice("content 01");
//        for (int i =1; i <=1; i++)
//            carList.add(car1);
        return carList;
    }

    public void refresh(String jsonData) {
        Log.d("msg3", jsonData);
        setCar(jsonData);
    }

   public void setCar(String jsonData) {
       try {
           JSONArray jsonArray = new JSONObject(jsonData)
                   .getJSONObject("result")
                   .getJSONObject("list")
                   .getJSONObject("car")
                   .getJSONObject("data")
                   .getJSONArray("carlist");
           carList.clear();
           for(int i = 0; i < jsonArray.length(); i++) {
               Car car = new Car();
               JSONObject jsonObject = jsonArray.getJSONObject(i);
               String style = jsonObject.getString("seriesname");
               String maxPrice = String.valueOf(jsonObject.getInt("seriesmaxprice")/10000.00);
               String minPrice = String.valueOf(jsonObject.getInt("seriesminprice")/10000.00);
               String price = minPrice+'-'+maxPrice+"万";
               Log.d("msg7", price);
               car.setStyle(style);
               car.setPrice(price);
               carList.add(car);
           }
           adapter.notifyDataSetChanged();
       } catch (JSONException e) {
           e.printStackTrace();
       }

   }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
